package com.homevip.controller.platform.user;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.homevip.core.controller.BaseController;
import com.homevip.core.page.Page;
import com.homevip.core.page.PageRequest;
import com.homevip.core.page.Sort;
import com.homevip.user.entity.User;
import com.homevip.user.entity.User_job;
import com.homevip.user.service.JobService;
import com.homevip.user.service.RoleService;
import com.homevip.util.StringUtil;
import com.homevip.util.UserSessionUtil;
import com.homevip.util.file.FileRwUtil;
import com.homevip.util.gson.JsonUtil;
import com.homevip.util.system.CommArray;
import com.homevip.util.system.Const;
import com.homevip.web.annotation.DataTypeAnnotation;
import com.homevip.web.annotation.DataTypeEnum;
import com.homevip.web.vo.ResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 管理人员页面
 * 
 * @author panpan
 */
@Controller
@RequestMapping("/platform/user/job/*")
public class PlatformUserJobController extends BaseController {
	@Autowired
	private JobService jobService;
	@Autowired
	private RoleService roleService;

	/**
	 * 职员列表
	 */
	@RequestMapping("/list")
	public void list(String keyword,String writer, Integer status, Integer pageno, HttpServletRequest request) {
		PageRequest pr = new PageRequest(null == pageno ? 0 : pageno, Const.DEFINE_PAGE);
		pr.setSort(new Sort("id", false));
		Page<User_job> page = jobService.findUserJob(keyword, status, pr);
		List<User_job> jobList =  jobService.findUserJob(writer);
		request.setAttribute("page", page);
	}

	/**
	 * 职员编辑
	 */
	@RequestMapping("/edit")
	public void edit(Integer id, HttpServletRequest request) throws Exception{
		Set<String> jobcodes = new HashSet<>();
		User_job item;
		if (null != id && id > 0) {
			item = jobService.getUserJobById(id);

			String[] codes = StringUtils.defaultString(item.getAcls()).split(",");
			for(String str : codes){
				if(!"".equals(StringUtils.defaultString(str))){
					jobcodes.add(str);
				}
			}
			request.setAttribute("item", item);
		}else{
			item = new User_job();
			item.setStatus(2);
		}
		String path = request.getSession().getServletContext().getRealPath("/");
		String menu = FileRwUtil.readFileReader(path + "/WEB-INF/res/job.json");
		JsonArray outarr = new JsonArray();
		JsonObject root = new JsonParser().parse(menu).getAsJsonObject();
		JsonArray ja1 = root.get("item").getAsJsonArray();
		for (JsonElement je1 : ja1) {
			JsonObject m1 = je1.getAsJsonObject();
			JsonObject jo = new JsonObject();
			jo.addProperty("id", m1.get("code").getAsString());
			jo.addProperty("pId", 0);
			jo.addProperty("name", m1.get("title").getAsString());
			jo.addProperty("open", true);
			outarr.add(jo);
			JsonArray ja2 = m1.get("item").getAsJsonArray();
			for (JsonElement je2 : ja2) {
				JsonObject m2 = je2.getAsJsonObject();
				jo = new JsonObject();
				jo.addProperty("id", m2.get("code").getAsString());
				jo.addProperty("pId", m1.get("code").getAsString());
				jo.addProperty("name", m2.get("title").getAsString());
				jo.addProperty("open", true);
				outarr.add(jo);
				JsonArray ja3 = m2.get("item").getAsJsonArray();
				for (JsonElement je3 : ja3) {
					JsonObject m3 = je3.getAsJsonObject();
					String code = m3.get("code").getAsString();
					jo = new JsonObject();
					jo.addProperty("id", code);
					jo.addProperty("pId", m2.get("code").getAsString());
					jo.addProperty("name", m3.get("title").getAsString());
					for (String str : jobcodes) {
						if(str.equals(code)){
							jo.addProperty("checked", true);
						}
					}
					outarr.add(jo);
				}
			}
		}
		request.setAttribute("item", item);
		request.setAttribute("zNodes", outarr.toString());
		request.setAttribute("arrIsforbid", CommArray.getInstance().getArr_Isforbid());
		request.setAttribute("roles", roleService.findUserRole(null, 2));
	}
	/**
	 * 用户编辑保存
	 */
	@RequestMapping("/ajax_edit")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxEdit(User_job userJob, HttpServletRequest request, HttpServletResponse response) throws Exception{
		ResultVo resultVo = new ResultVo();
		User user = UserSessionUtil.getUser(request);
		try {
			jobService.saveUserJob(userJob,user.getCname());
		} catch (Exception e) {
			resultVo = new ResultVo(false,"编辑失败");
		}
		request.setAttribute("writer", user.getCname());
		writeJson(response, JsonUtil.toJson(resultVo));
	}

	/**
	 * 工作删除
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/ajax_delete")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxDelete(Integer _action_id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultVo resultVo = new ResultVo();
		User_job userJob;
		if(null != _action_id && _action_id > 0){
			userJob = jobService.getUserJobById(_action_id);
			try {
				jobService.deleteUserJob(userJob);
			} catch (Exception e) {
				resultVo = new ResultVo(false,e.getMessage());
			}
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}

	/**
	 * 状态更新状态
	 * 
	 * @param _action_id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/ajax_updatestatus")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxUpdateStatus(Integer _action_id, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ResultVo resultVo = new ResultVo();
		User_job userJob;
		if (null != _action_id && _action_id > 0) {
			userJob = jobService.getUserJobById(_action_id);
			try {
				userJob.setStatus(userJob.getStatus() <= 1 ? 2 : 1);
				jobService.saveUserJob(userJob);
			} catch (Exception e) {
				resultVo = new ResultVo(false, "更新失败");
			}
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}

}
